export const REQUEST_STATUSES = ['notStart', 'pending', 'succeed', 'fail'];

export const TASK_STATES = [
  'waiting',
  'inqueue',
  'processing',
  'stopping',
  'stopped',
  'finished',
  'fail',
  'deleting'
];

export const WAITING_TASK_STATES = ['waiting', 'paused', 'stopped'];

export const PROCESSING_TASK_STATES = ['processing', 'stopping', 'deleting'];

export const FINISHED_TASK_STATES = ['finished', 'fail'];

export const SOCKET_IO_PATH = '/socket.io';

export const IP_RE = new RegExp(
  [
    '^',
    '(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\.',
    '(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\.',
    '(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\.',
    '(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])',
    '$'
  ].join('')
);

export const IP_V6 = new RegExp(
  /^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0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d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*)(\/(([1-9])|([1-9][0-9])|(1[0-1][0-9]|12[0-8]))){0,1})*$/
);

export const IP_ADDRESS_MASK = new RegExp(
  /^(254|252|248|240|224|192|128|0)\.0\.0\.0|255\.(254|252|248|240|224|192|128|0)\.0\.0|255\.255\.(254|252|248|240|224|192|128|0)\.0|255\.255\.255\.(255|254|252|248|240|224|192|128|0)$/
);

export const IP_RANGE_RE = new RegExp(
  [
    '^',
    '(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\.',
    '(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\.',
    '(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\.',
    '(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])',
    '-',
    '(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])',
    '$'
  ].join('')
);

IP_RANGE_RE.test = ipRange => {
  if (!RegExp.prototype.test.call(IP_RANGE_RE, ipRange)) {
    return false;
  }
  const splices = ipRange.split(/\.|-/g) || [];
  if (
    isNaN(Number(splices[3])) ||
    isNaN(Number(splices[4])) ||
    Number(splices[3]) >= Number(splices[4])
  ) {
    return false;
  }
  return true;
};

export const IP_INTERVAL_RE = new RegExp(
  [
    '^',
    '(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\.',
    '(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\.',
    '(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\.',
    '(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])',
    '-',
    '(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\.',
    '(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\.',
    '(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\.',
    '(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])',
    '$'
  ].join('')
);

IP_INTERVAL_RE.test = (ipRange, limi = false) => {
  if (!RegExp.prototype.test.call(IP_INTERVAL_RE, ipRange)) {
    return false;
  }
  const splices = ipRange.split(/\.|-/g) || [];
  if (
    isNaN(Number(splices[3])) ||
    isNaN(Number(splices[7])) ||
    (Number(splices[3]) >= Number(splices[7]) && !limi)
  ) {
    return false;
  }
  return true;
};

export const CIDR_RE = new RegExp(
  [
    '^',
    '(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\.',
    '(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\.',
    '(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\.',
    '(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])',
    '(\\/\\d|\\/[1,2]\\d|\\/3[0-2])',
    '$'
  ].join('')
);

export const IPV4_CIDE_RE = new RegExp(
  '^((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))\\/([0-9]|[1-2][0-9]|3[0-2])$'
);

// eslint-disable-next-line no-useless-escape
export const IPV6_CIDE_RE = new RegExp(
  '^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}|[1-5]\d{4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5])$/;

export const PORT_RANGE_RE = new RegExp(
  [
    '^',
    '([0-9]|[1-9]\\d{1,3}|[1-5]\\d{4}|6[0-4]\\d{3}|65[0-4]\\d{2}|655[0-2]\\d|6553[0-5])',
    '-',
    '([0-9]|[1-9]\\d{1,3}|[1-5]\\d{4}|6[0-4]\\d{3}|65[0-4]\\d{2}|655[0-2]\\d|6553[0-5])',
    '$'
  ].join('')
);

//匹配135 6666 7777类型,即在第三位、第七位之后有可选的空格
export const PHONE_RE = /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])(\s)?\d{4}(\s)?\d{4}$/;

export const EMAIL_RE = /^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$/;

// eslint-disable-next-line no-useless-escape
export const PASSWORD_STRICT_RE = /^(?=.*[a-zA-Z])(?=.*\d)(?=.*[~!@#$%^&*()_+\-={}:";'<>?,\.]).{8,16}$/;

PORT_RANGE_RE.test = portRange => {
  if (!RegExp.prototype.test.call(PORT_RANGE_RE, portRange)) {
    return false;
  }
  const ports = portRange.split('-');
  if (isNaN(Number(ports[0])) || isNaN(Number(ports[1])) || Number(ports[0]) >= Number(ports[1])) {
    return false;
  }
  return true;
};

export const URL_RE = /[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+\.?/;

export const FULL_URL_RE = /http(s)?:\/\/[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+\.?/;

export const NAME_RE = /^[\u4e00-\u9fa5a-zA-Z0-9-、—\\_/：~(\s)]+$/;

export const CUSTOM_NAME_RE = /^[\u4e00-\u9fa5a-zA-Z0-9-_]+$/;

export const USER_NAME_RE = /^[a-zA-Z0-9]+$/;

export const ENG_NAME = /^[a-zA-Z0-9-._!@#$%^&*()_+]+$/;

export const REAL_NAME_RE = /^[\u4E00-\u9FA5A-Za-z0-9]+$/;

export const DOMAIN_RE = /[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+\.?[a-zA-Z][-a-zA-Z]{0,62}/;

export const TASK_MODE_MAP = {
  deep: '精准',
  fast: '极速'
};

export const TASK_COMPARE_TYPES = ['appear', 'disappear', 'historical'];

export const LEVEL_TYPES = ['fatal', 'high', 'middle', 'low'];

export const LEVEL_TEXT = {
  fatal: '严重',
  high: '高危',
  middle: '中危',
  low: '低危'
};

export const CONSOLE_LEVEL_ICON_MAP = {
  fatal: {
    text: '重',
    style: { color: '#BB0707', background: 'rgba(187,7,7, .2)' }
  },
  high: {
    text: '高',
    style: { color: '#F04038', background: 'rgba(255,0,0, .2)' }
  },
  middle: {
    text: '中',
    style: { color: '#FAAF2B', background: 'rgba(246,126,35, .2)' }
  },
  low: {
    text: '低',
    style: { color: '#FED831', background: 'rgba(254,216,49, .2)' }
  },
  disable: {
    style: { color: '#999EA8', background: '#E6EAEF' }
  }
};

//漏洞类型
export const VUL_TYPES = {
  system: '系统漏洞',
  web: 'web漏洞'
};

export const CONSOLE_ONEDAY_DISABLE_COLOR = {
  color: '#CBD1DA'
};

export const LEVEL_COLOR = {
  fatal: '#a00000',
  high: '#ee3e41',
  middle: '#f67e23',
  low: '#fed831'
};

export const TASK_TYPE_MAP = {
  Zoomeye: '资产探测',
  OneDay: '1DAY检测',
  WeakPassword: '弱口令',
  Web: 'Web指纹探测',
  Honeypot: '蜜罐探测',
  VulDetect: '漏洞普查'
};

export const TIME_UNIT_MAP = {
  hour: '时',
  day: '天',
  week: '周',
  month: '月',
  year: '年'
};

/* eslint-disable  camelcase */
export const WEB_FINGER_TITLE_MAP = {
  web_titles: '网站标题',
  web_app_and_versions: 'web应用',
  web_server_and_versions: 'web容器',
  web_framwork_and_versions: 'web框架',
  web_waf_and_versions: '防护信息',
  web_db_and_versions: '数据库',
  web_languages: '开发语言'
};
/* eslint-enable */

// 响应白名单，该名单下请求不做(response.data.result === true)校验
export const WHITE_RESPONSE_URLS = [
  '/api/site/downloadChangeForm',
  '/api/site/downloadReport',
  '/api/site/downloadForm',
  '/api/site/import-failed-sites-form',
  '/api/log/downloadLogin',
  '/api/log/downloadOperate',
  '/api/task/downloadDetectionTask',
  '/api/task/downloadOnedayTask',
  '/api/task/downloadOnedayTaskReport',
  '/api/task/downloadZoomeyeTaskReport',
  '/api/task/downloadWebTask',
  '/api/task/downloadWebTaskReport',
  '/api/site/downloadAnalysisReport',
  '/api/ksp/upgrade',
  '/maps',
  '/location.json',
  '/api/task/attachment',
  '/api/task/detection/docx/download',
  '/api/task/detection/xlsx/download',
  '/api/external/poc/download',
  '/api/system/download-failed-excel',
  '/api/task/dict/download',
  '/api/task/detection/vul/docx/download',
  '/api/task/detection/vul/xlsx/download',
  '/api/v4/external/download',
  '/api/ksp/cluster/upgrade'
];

/**
 * ==============授权相关常量==============
 */

// 模块对应的默认路由，越权访问后跳转会默认路由
export const MODULE_DEFAULT_ROUTE_MAP = {
  // "statics", //综合分析
  'asset-detection': '/task-manage',
  'asset-manage': '/asset-manage',
  'asset-config': '/asset-config/target-manage',
  '1day': '/vul/oneday-detection',
  // "knowledge", //知识库
  'task-manage': '/task-manage',
  'log-manage': '/log-manage/login-log',
  user: '/asset-config/organization',
  notice: '/notify',
  personal: '/account-setting',
  system: '/system-config/user-manage',
  about: '/product-information'
};

// 用户登录时，进入首页优先选择的模块
export const PRIORITY_MODULES = ['asset-manage', 'log-manage', 'personal'];

// 一级模块与二级模块分割符
export const MODULE_SPLIT = '|';

// 针对路由定义权限，路由与react router中定义方式一致，采用严格模式(exact=true)
// expmple:
// {
//   '/system-center/user-manage': 'user-manage'
// }
export const ROUTE_MODULE_MAP = {};

export const ROUTE_MODULES = Object.keys(ROUTE_MODULE_MAP);

export const POWERS = ['create', 'modify', 'delete', 'view'];

/**
 * ==============授权相关常量==============
 */

export const MAX_DETECTION_PORT = 65536;

export const MAX_DEEP_DETECTION_COUNT = 256 * 256;

export const MAX_FAST_DETECTION_COUNT = 256 * 256 * 128;

// 同时下发1day数最大值
export const MAX_BATCH_ADD_ONEDAY_COUNT = 20;

export const UPDATING_STAGE_PROCESS_MAP = {
  pre: 30,
  install: 50,
  post: 99,
  rollback: 99
};

export const MAX_UPDATING_ERROR_TIMES = 5;

export const ORGANIZATION_RE = /^[a-zA-Z0-9\u4e00-\u9fa5]{1,16}$/;

export const ORGANIZATION_RE32 = /^[a-zA-Z0-9\u4e00-\u9fa5]{1,32}$/;

export const NODE_NAME = {
  master: '调度设备',
  storage: '存储设备',
  slave: '引擎设备'
};

export const LEVEL_SCORE = {
  fatal: 20,
  high: 10,
  middle: 5,
  low: 1
};

export const ORIGIN_DESC_MAP = {
  hand: '人工添加',
  active: '主动探测',
  switch: '交换机采集',
  'zoomeye.org': '公网查询',
  passive: '被动探测',
  uyun_CMDB: '优云CMDB'
};

export const ADD_BY_KEY = {
  hand: 'hand',
  detection: 'active',
  ping: 'active',
  /* eslint-disable */
  external_oneday: 'hand',
  external_web: 'hand',
  /* eslint-enable */
  switch: 'unactive'
};

export const ADD_BY_MAP = {
  active: ['detection', 'ping'],
  unactive: ['switch'],
  hand: ['hand', 'external_oneday', 'external_web']
};

export const TASK_PRIORITY_MAP = {
  low: '低',
  middle: '中',
  high: '高'
};

export const TASK_PROTOCOL_MAP = {
  icmp: 'ICMP',
  udp: 'UDP',
  tcp: 'TCP'
};

// 资产探测IP存活列表常量定义
export const IP_ALIVE_LIST_MAP = {
  LOCATION_FIELDS_MAPPING: {
    'location.lat': '纬度',
    'location.lon': '经度',
    accuracy: '地理位置精度',
    continent: '大洲',
    country: '国家',
    province: '省',
    city: '城市',
    county: '区/县',
    timeStamp: '探测存活时间'
  },
  TAG_FIELDS_MAPPING: {
    'detail.timezone': '时区',
    'detail.scene.cn': '应用场景',
    'detail.asn_number': 'ASN',
    'detail.zip_code': '邮政编码',
    'detail.owner.cn': '组织',
    'detail.isp_domain.cn': 'ISP',
    'detail.user_type.cn': 'IP使用者类型',
    'detail.user.cn': 'IP使用者'
  },
  BASE_HEADER: ['location.lat', 'location.lon', 'timeStamp'],
  LOCATION_FIELDS: [
    'location.lat',
    'location.lon',
    'accuracy',
    'continent',
    'country',
    'province',
    'city',
    'county',
    'timeStamp'
  ],

  /* eslint-disable camelcase*/
  PORT_INFO: {
    port: '端口',
    protocol: '协议',
    'xmap.rdns': '反向域名解析',
    'xmap.device': '设备类型',
    device_type: '设备类型',
    brand: '品牌',
    service: '服务',
    version: '版本号',
    product_name: '产品名称',
    extra_info: '额外信息',
    product_company_name: '公司名称',
    ssl: 'SSL',
    'productInfo.brand.name.cn': '品牌名称',
    'productInfo.name.en': '产品名称',
    'productInfo.name.cn': '产品名称',
    'productInfo.company.name.cn': '公司名称',
    'productInfo.component.info.name.cn': '组件详情',
    'productInfo.component.type.name.cn': '类型信息',
    'wmap.wmap_extra.wmap_db_name': '数据库',
    'wmap.wmap_extra.wmap_language_name': 'Web语言',
    'wmap.wmap_extra.wmap_webapp_name': 'Web应用',
    'wmap.title': '标题',

    'wmap.component.name': '站点组件',
    'wmap.wmap_extra.wmap_waf_name': 'waf',
    // eslint-disable-next-line no-dupe-keys
    'wmap.wmap_extra.wmap_component_name': 'Web组件',
    // 'wmap.db.name': 'Web数据库',
    'wmap.wmap_extra.wmap_framework_name': 'Web框架',
    'wmap.wmap_extra.wmap_frontend_name': 'Web前端',
    // 'wmap.language.name': 'Web语言',
    // 'wmap.webapp.name': 'Web应用'
    'xmap.brand': '品牌',
    'xmap.version': '版本号',
    'xmap.product': '产品名称',
    'xmap.extra_info': '额外信息',
    'xmap.company': '公司名称'
    // 'xmap.ssl': 'SSL'
  },
  /* eslint-enable camelcase*/

  // web由多个字段组合而成, 需要单独处理
  PORT_EXPAND_INFO: ['banner', 'ssl', 'web']
};
